<?php


class ShopCartService extends TP_Service {
    public $redis;
    public function __construct() {
        parent::__construct();
        $this->redis = $this->newRedis();
    }

	public function uuid()
	{
		$chars = md5(uniqid(mt_rand(), true));
		$uuid = substr ( $chars, 0, 8 ) . '-'
			. substr ( $chars, 8, 4 ) . '-'
			. substr ( $chars, 12, 4 ) . '-'
			. substr ( $chars, 16, 4 ) . '-'
			. substr ( $chars, 20, 12 );
		return $uuid ;
	}

    /**
     * 方法名 cartList
     *
     * @Name 获取方法列和角色方法列
     * @Desc 购物车列表
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     */
    public function cartList($param) {
        if (!isset($param['uid'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }

        $list = $this->redis->hgetAll("pattern:shopcat:" . $param['uid']);

        $res = array();
        $index = 0;
        foreach ($list as $key => $v) {
            if (strpos($key, 'info') !== false) {
                $info = json_decode($v, true);
                $num = 'num:' . $info['id'];
                $info['num'] = $list[$num];

                $res[$index++] = $info;
            }
        }

        $output = [];
		$list2 = $this->redis->lRange("pattern:cart:" . $param['uid'], 0, -1);
		foreach ($list2 as $item) {
			$info = json_decode($item, true);
			$info['num'] = 1;
			$output[] = $info;
		}

//        return $res;
		return $output;
    }



    /**
     * 方法名 cartAdd
     *
     * @Name 获取方法列和角色方法列
     * @Desc 购物车添加
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     */
    public function cartAdd($param) {
        if (!isset($param['uid'], $param['pid'], $param['num'], $param['info'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }
        if ($param['num'] <= 0) {
            exit(retJson(ErrCode::ErrCode_10602));
        }

        $hash = $this->getKeyField($param['uid'], $param['pid']);

        try {
            
//            $status = $this->redis->hsetnx($hash['key'], $hash['num'], $param['num']);
//            if (!$status) {
//                $status = $this->redis->hset($hash['key'], $hash['num'], $param['num']);
//            }
//            $this->redis->hsetnx($hash['key'], $hash['info'], $param['info']);


            // 新方法
			$info = json_decode($param['info'], true);
			$info['uuid'] = $this->uuid();
            $this->redis->lPush($hash['key1'], json_encode($info));
        } catch (\Throwable $th) {
            log_info($th);
            exit(retJson(ErrCode::ErrCode_10601));
        }

        return true;
    }


    /**
     * 方法名 cartDelete
     *
     * @Name 获取方法列和角色方法列
     * @Desc 购物车删除
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     */
    public function cartDelete($param) {
        if (!isset($param['index'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }

        $hash = $this->getKeyField($param['uid'], $param['pid']);

        try {
        	$info = $this->redis->lIndex($hash['key1'], $param['index']);

        	$this->redis->lRem($hash['key1'], $info, 1);

//            $this->redis->hdel($hash['key'], $hash['num'], $hash['info']);
        } catch (\Throwable $th) {
            log_info($th);
            exit(retJson(ErrCode::ErrCode_10601));
        }

        return true;
    }


    private function getKeyField($uid, $pid) {
        $key  = "pattern:shopcat:" . $uid;
        $num  = "num:" . $pid;
        $info = "info:" . $pid;
        $key1 = "pattern:cart:" . $uid;
        return ["key" => $key, "key1" => $key1, "num" => $num, "info" => $info];
    }


    /**
     * 方法名 cartNum
     *
     * @Name 获取方法列和角色方法列
     * @Desc 购物车商品数量
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     */
    public function cartNum($param) {
        if (!isset($param['uid'], $param['pid'], $param['num'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }
        if ($param['num'] <= 0) {
            exit(retJson(ErrCode::ErrCode_10602));
        }

        $hash = $this->getKeyField($param['uid'], $param['pid']);
        try {
            $this->redis->hset($hash['key'], $hash['num'], $param['num']);
        } catch (\Throwable $th) {
            log_info($th);
            exit(retJson(ErrCode::ErrCode_10601));
        }

        return true;
    }
}
